import 'package:flutter/material.dart';
import 'package:trip_flutter/util/screen_adapt_helper.dart';

class Searchlistwidget extends StatefulWidget {
  final List<String> cityList;
  const Searchlistwidget({super.key, required this.cityList});

  @override
  State<Searchlistwidget> createState() => _SearchlistwidgetState();
}

class _SearchlistwidgetState extends State<Searchlistwidget> {
  List<bool> _itemHoverStates = [];

  @override
  void initState() {
    super.initState();
    _initHoverStates();
  }

  @override
  void didUpdateWidget(covariant Searchlistwidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.cityList != oldWidget.cityList) {
      _initHoverStates();
    }
  }

  void _initHoverStates() {
    setState(() {
      _itemHoverStates = List.filled(widget.cityList.length, false);
    });
  }

  @override
  Widget build(BuildContext context) {
    if (widget.cityList.isEmpty) {
      return const Center(child: Text("暂无城市数据"));
    }
    return ListView.builder(
      itemCount: widget.cityList.length,
      itemBuilder: (context, index) {
        if (_itemHoverStates.length != widget.cityList.length) {
          _initHoverStates();
        }
        final isItemHovered = _itemHoverStates[index];
        return Container(
          margin: EdgeInsets.only(left: 12.px, right: 12.px),
          child: MouseRegion(
            onEnter: (_) => setState(() => _itemHoverStates[index] = true),
            onExit: (_) => setState(() => _itemHoverStates[index] = false),
            child: Container(
              alignment: Alignment.bottomLeft,
              decoration: BoxDecoration(
                border: const Border(
                  bottom: BorderSide(color: Colors.grey, width: 1),
                ),
                // 核心修复：非hover时设置boxShadow为null（无阴影）
                boxShadow: isItemHovered
                    ? [
                        // hover时有阴影
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.5),
                          spreadRadius: 2,
                          blurRadius: 8,
                          offset: const Offset(0, 4),
                        ),
                      ]
                    : null, // 非hover时无阴影
              ),
              child: ListTile(
                title: Text(
                  widget.cityList[index],
                  style: TextStyle(color: Colors.blue, fontSize: 16.px),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
